//
//  WKShareView.m
//  WeKeTang
//
//  Created by ichina on 2017/9/8.
//  Copyright © 2017年 Dezhi Tiancheng Education Technology (Beijing) Co. Ltd. All rights reserved.
//

#import "WKShareView.h"
#import "NSAttributedString+Addition.h"

@interface WKShareView ()

@property (nonatomic, strong) UIView *shareView;

@property (nonatomic, strong) YYLabel *titleLabel;

@property (nonatomic, strong) UIView  *lineView;

@property (nonatomic, strong) UIButton *btnCancel;

@property (nonatomic, strong) NSMutableArray *btnArray;

@end

@implementation WKShareView


- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
       
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    
    [self addSubview:self.shareView];
    [self.shareView addSubview:self.titleLabel];
    [self.shareView addSubview:self.lineView];
    [self.shareView addSubview:self.btnCancel];
    
    self.btnArray = [NSMutableArray array];
    NSArray *titles = @[@"QQ好友",@"QQ空间",@"微信朋友圈",@"微信朋友"];
    NSArray *imageNames = @[@"icon_qqfriends",@"icon_qqzone",@"icon_weixinfriend",@"icon_weixinhaoyou"];
    
    for (NSInteger i = 0; i<titles.count; i++) {
        NSString *title = titles[i];
    
        NSString *imageName = imageNames[i];
        UIButton *button = [self creatButtonWithImageName:imageName andButtonTitle:title];
        [self.btnArray addObject:button];
        [self.shareView addSubview:button];
    }
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat x ;
    CGFloat y = CGRectGetMaxY(self.titleLabel.frame)+20;
    CGFloat W = (kDeviceScreenWidth - 10)/4;
    CGFloat H = W;
    for (NSInteger i = 0; i<self.btnArray.count; i++) {
        
        UIButton *button = self.btnArray[i];
        x = 5+i*W;
        button.frame = CGRectMake(x, y, W, H);
        button.tag = i;
        [button addTarget:self action:@selector(selectShareBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
}
#pragma mark - 选择分享类型
- (void)selectShareBtnAction:(UIButton*)sender{
    
    
    
    if ([self.delegate respondsToSelector:@selector(WKShareView:buttonIndex:)]) {
        [self.delegate WKShareView:self buttonIndex:sender.tag];
    }
}
- (void)cancelAction{
    
    if ([self.delegate respondsToSelector:@selector(WKShareView:WithCancelAction:)]) {
        
        [self.delegate WKShareView:self WithCancelAction:nil];
    }
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self removeFromSuperview];
}
- (UIView *)shareView{
    
    if (!_shareView) {
        
        _shareView = [[UIView alloc]initWithFrame:CGRectMake(0, kDeviceScreenHeight-200, kDeviceScreenWidth, 200)];
        _shareView.backgroundColor = [UIColor whiteColor];
    }
    return _shareView;
}
- (YYLabel *)titleLabel{
   
    if (_titleLabel == nil) {
       
        _titleLabel = [[YYLabel alloc]initWithFrame:CGRectMake(0, 15, kDeviceScreenWidth, 21)];
        _titleLabel.text = @"分享到";
        _titleLabel.textColor = [UIColor colorWithHexString:@"#666666"];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:15];
        
    }
    return _titleLabel;
}
- (UIView *)lineView{
    
    if (_lineView == nil) {
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 200-49.5, kDeviceScreenWidth, 0.5)];
        _lineView.backgroundColor = [UIColor colorWithHexString:@"#666666"];
    }
    return _lineView;
}
- (UIButton *)btnCancel{
    
    if (_btnCancel == nil) {
        _btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnCancel setTitle:@"取消" forState:UIControlStateNormal];
        _btnCancel.frame = CGRectMake(0, 200-50, kDeviceScreenWidth, 50);
        [_btnCancel setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        _btnCancel.titleLabel.font = [UIFont systemFontOfSize:15];
        [_btnCancel addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnCancel;
}
- (UIButton*)creatButtonWithImageName:(NSString*)imageName andButtonTitle:(NSString*)title{
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
    
    NSAttributedString *attrStr = [NSAttributedString imageTextWithImage:[UIImage imageNamed:imageName] imageWH:40 title:title fontSize:13 titleColor:[UIColor colorWithHexString:@"#333333"] spacing:8];
    button.titleLabel.numberOfLines = 0;
    
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [button setAttributedTitle:attrStr forState:UIControlStateNormal];
    
    return button;
    
}

@end
